﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TPCN.DAL;
using TPCN.Common;
using System.Web.Security;

namespace TPCN.Admincp
{
    public partial class Hoadonnhap : System.Web.UI.Page
    {
        private static string HoadonnhapID = String.Empty;
        private static string ChitietHoadonnhapID = String.Empty;
        private static bool inDetail = false;
        private static List<HoaDonNhapObj> source;
        private static List<ChiTietHoaDonNhapObj> listhang;
        private static HoaDonNhapObj obj;
        private static ChiTietHoaDonNhapObj detail;

        private const string UPDATE = "Cập nhật";
        private const string INSERT = "Thêm";
        private const string CMD_EDIT = "cmdEdit";
        private const string CMD_DELETE = "cmdDelete";
        private const string CMD_DETAIL = "cmdViewDetail";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGridView();
                InitForm();
            }
        }

        private void InitForm()
        {
            EditFormPanel.Visible = false;
            GridViewPanel.Visible = true;
            ButtonPanel.Visible = true;
            DetailGridPanel.Visible = false;
            DetailEditPanel.Visible = false;
            txtNhanvien.Text = User.Identity.Name;
            LoadcboProduct();

            inDetail = false;
        }

        private void BindGridView()
        {
            source = new List<HoaDonNhapObj>();
            source = HoaDonNhapDAL.GetByAll();
            HDNGridView.DataSource = source;
            HDNGridView.DataBind();
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            if (inDetail)
            {
                DetailGridPanel.Visible = false;
                DetailEditPanel.Visible = true;
                ButtonPanel.Visible = false;
                DetailBtnSave.Text = INSERT;
            }
            else
            {
                TurnPanelOnOff();
                btnSave.Text = INSERT;
                cboNgaylap.SelectedDate = DateTime.Now;
            }

        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (inDetail)
            {
                for (int i = 0; i < DetailGridView.Rows.Count; i++)
                {
                    GridViewRow row = DetailGridView.Rows[i];
                    if (row.RowType == DataControlRowType.DataRow)
                    {
                        CheckBox chk = (CheckBox)row.FindControl("chkSelect");
                        if (chk.Checked)
                        {
                            string ID = row.Cells[1].Text;
                            ChiTietHoaDonNhapDAL.Delete(ID);
                        }
                    }
                }
                DetailGridView.PageIndex = 0;
                LoadDetailPanel();
            }
            else
            {
                for (int i = 0; i < HDNGridView.Rows.Count; i++)
                {
                    GridViewRow row = HDNGridView.Rows[i];
                    if (row.RowType == DataControlRowType.DataRow)
                    {
                        CheckBox chk = (CheckBox)row.FindControl("chkSelect");
                        if (chk.Checked)
                        {
                            string ID = row.Cells[1].Text;
                            HoaDonNhapDAL.Delete(ID);
                            ChiTietHoaDonNhapDAL.DeleteByHoadonnhapID(ID);
                        }
                    }
                }
                HDNGridView.PageIndex = 0;
                BindGridView();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                if (inDetail)
                {
                    SetValueFromDetailForm();
                    switch (DetailBtnSave.Text)
                    {
                        case UPDATE:
                            ChiTietHoaDonNhapDAL.Update(detail);
                            LoadDetailPanel();
                            break;
                        case INSERT:
                            ChiTietHoaDonNhapDAL.Insert(detail);
                            LoadDetailPanel();
                            break;
                    }
                    ControlHelper.ResetControl(DetailEditPanel);
                    DetailEditPanel.Visible = false;
                    DetailGridPanel.Visible = true;
                    ButtonPanel.Visible = true;
                    LoadDetailPanel();
                    detail = null;
                }
                else
                {
                    SetValueFromForm();
                    switch (btnSave.Text)
                    {
                        case UPDATE:
                            HoaDonNhapDAL.Update(obj);
                            BindGridView();
                            break;
                        case INSERT:
                            HoaDonNhapDAL.Insert(obj);
                            BindGridView();
                            break;
                    }
                    ControlHelper.ResetControl(EditFormPanel);
                    TurnPanelOnOff();
                    obj = null;
                    Page.Response.Redirect(Request.RawUrl, true);
                }
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            if (inDetail)
            {
                ControlHelper.ResetControl(DetailEditPanel);
                DetailEditPanel.Visible = false;
                DetailGridPanel.Visible = true;
                ButtonPanel.Visible = true;
            }
            else
            {
                TurnPanelOnOff();
                ControlHelper.ResetControl(EditFormPanel);
                Page.Response.Redirect(Request.RawUrl, true);
            }

        }

        protected void HDNGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            HDNGridView.PageIndex = e.NewPageIndex;
            BindGridView();
        }

        protected void HDNGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            HoadonnhapID = e.CommandArgument.ToString();
            switch (e.CommandName)
            {
                case CMD_EDIT:
                    obj = new HoaDonNhapObj();
                    obj = HoaDonNhapDAL.GetByID(HoadonnhapID);
                    FillValueToForm(obj);
                    btnSave.Text = UPDATE;
                    TurnPanelOnOff();
                    break;
                case CMD_DELETE:
                    HoaDonNhapDAL.Delete(HoadonnhapID);
                    ChiTietHoaDonNhapDAL.DeleteByHoadonnhapID(HoadonnhapID);
                    BindGridView();
                    break;
                case CMD_DETAIL:
                    LoadDetailPanel();
                    inDetail = true;
                    DetailGridPanel.Visible = true;
                    GridViewPanel.Visible = false;
                    break;
            }
        }

        protected void LoadDetailPanel()
        {
            listhang = new List<ChiTietHoaDonNhapObj>();
            listhang = ChiTietHoaDonNhapDAL.GetByHoadonnhapID(HoadonnhapID);
            DetailGridView.DataSource = listhang;
            DetailGridView.DataBind();
        }

        private void TurnPanelOnOff()
        {
            GridViewPanel.Visible = !GridViewPanel.Visible;
            EditFormPanel.Visible = !EditFormPanel.Visible;
            ButtonPanel.Visible = !ButtonPanel.Visible;
        }

        private void FillValueToForm(HoaDonNhapObj obj)
        {
            cboNgaylap.SelectedDate = Convert.ToDateTime(obj.Ngaylap);
            txtNhanvien.Text = User.Identity.Name;
        }

        protected void SetValueFromForm()
        {
            if (obj == null)
                obj = new HoaDonNhapObj();
            obj.Ngaylap = cboNgaylap.SelectedDate.Value.ToShortDateString();
            obj.Nhanvien = User.Identity.Name;
        }

        protected string FormatDatetime(Object o)
        {
            DateTime time = Convert.ToDateTime(o);
            return time.ToString("dd/MM/yyyy");
        }

        protected string GetProductNameById(Object id)
        {
            string mID = Convert.ToString(id);
            return HangDAL.GetByID(mID).Tenhang;
        }

        protected void LoadcboProduct()
        {
            List<HangObj> products = new List<HangObj>();
            products = HangDAL.GetByAll();
            cboProduct.DataSource = products;
            cboProduct.DataTextField = "Tenhang";
            cboProduct.DataValueField = "HangID";
            cboProduct.DataBind();
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            GridViewPanel.Visible = true;
            DetailGridPanel.Visible = false;
            inDetail = false;
        }

        protected void SetValueFromDetailForm()
        {
            if (detail == null)
                detail = new ChiTietHoaDonNhapObj();
            detail.Dongia = txtPrice.Text;
            detail.HangID = cboProduct.SelectedValue;
            detail.Soluong = txtQuantity.Text;
            detail.HoadonnhapID = HoadonnhapID;
        }

        protected void DetailGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            ChitietHoadonnhapID = e.CommandArgument.ToString();
            detail = new ChiTietHoaDonNhapObj();
            detail = ChiTietHoaDonNhapDAL.GetByID(ChitietHoadonnhapID);
            switch (e.CommandName)
            {
                case CMD_EDIT:
                    FillValueToDetailForm(detail);
                    DetailBtnSave.Text = UPDATE;
                    DetailEditPanel.Visible = true;
                    DetailGridPanel.Visible = false;
                    ButtonPanel.Visible = false;
                    break;
                case CMD_DELETE:
                    ChiTietHoaDonNhapDAL.Delete(ChitietHoadonnhapID);
                    LoadDetailPanel();
                    break;
            }
        }

        private void FillValueToDetailForm(ChiTietHoaDonNhapObj detail)
        {
            cboProduct.SelectedValue = detail.HangID;
            txtQuantity.Text = detail.Soluong;
            txtPrice.Text = detail.Dongia;
        }

        protected void DetailGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            DetailGridView.PageIndex = e.NewPageIndex;
            LoadDetailPanel();
        }
    }
}